package edu.tyut.lingyunxing.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import edu.tyut.lingyunxing.common.R;
import edu.tyut.lingyunxing.dto.FlightDTO;
import edu.tyut.lingyunxing.pojo.Airport;
import edu.tyut.lingyunxing.pojo.Flight;
import edu.tyut.lingyunxing.pojo.Ticket;
import edu.tyut.lingyunxing.service.AirportService;
import edu.tyut.lingyunxing.service.FlightService;
import edu.tyut.lingyunxing.service.TicketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * DateTime: 2025/6/25 - 0:42
 * Author: Ycs
 */
@RestController
@RequestMapping("/flight")
@Slf4j
public class FlightController {

    @Resource
    private FlightService service;

    @Resource
    private AirportService airportService;
    
    @Resource
    private TicketService ticketService;

    @PostMapping("/add")
    public R<String> add(@RequestBody Flight flight, HttpServletRequest request) {
        boolean b = service.save(flight);
        return b ? R.success("添加成功") : R.error("添加失败");
    }

    @PostMapping("/list")
    public R<List<FlightDTO>> list(HttpServletRequest request) {
        List<FlightDTO> res = new ArrayList<>();
        List<Flight> list = service.list();
        for (Flight f : list) {
            
            Airport from = airportService.getById(f.getFromAirport());
            String fromAirport = from.getName();
            Airport to = airportService.getById(f.getToAirport());
            String toAirport = to.getName();
            
            LambdaQueryWrapper<Ticket> q = new LambdaQueryWrapper<>();
            q.eq(Ticket::getFlight, f.getId())
                    .eq(Ticket::getSeatType, 0);
            List<Ticket> economy = ticketService.list(q);
            int economySize = economy.size();
            
            LambdaQueryWrapper<Ticket> qb = new LambdaQueryWrapper<>();
            qb.eq(Ticket::getFlight, f.getId())
                    .eq(Ticket::getSeatType, 1);
            List<Ticket> business = ticketService.list(q);
            int businessSize = economy.size();

            FlightDTO dto = FlightDTO.builder()
                    .id(f.getId())
                    .fromAirport(fromAirport)
                    .toAirport(toAirport)
                    .economyPrice((float) ((float) f.getEconomyPrice() / 100.00))
                    .economySeats(f.getEconomySeats() - economySize)
                    .businessPrice((float) ((float) f.getBusinessPrice() / 100))
                    .businessSeats(f.getBusinessSeats() - businessSize)
                    .flyTime(f.getFlyTime())
                    .endTime(f.getEndTime())
                    .build();
            res.add(dto);
        }
        return R.success(res);
    }


}
